//
//  ReplyView.m
//  MeiXiu
//
//  Created by Yuan on 12-11-27.
//
//

#import "ReplyView.h"
#import "UIButton+WebCache.h"
#import <QuartzCore/QuartzCore.h>

@implementation ReplyViewCell
@synthesize iconButton;//头像
@synthesize aliasLable; //昵称
@synthesize timeLbl; //时间
@synthesize contentLbl;//评论


-(void)dealloc
{
    [aliasLable release];aliasLable = nil;
    [timeLbl release];timeLbl = nil;
    [contentLbl release];contentLbl = nil;
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
//        self.backgroundColor = [UIColor greenColor];
//        self.layer.borderColor = [UIColor darkGrayColor].CGColor;
//        self.layer.borderWidth = 1.0f;
        
        CGFloat margin_top = 15.0f;
        
        iconButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [iconButton setFrame:CGRectMake(10,margin_top, 40,40)];
        iconButton.layer.cornerRadius = 6.0;
        [iconButton setUserInteractionEnabled:YES];
        iconButton.layer.masksToBounds = YES;
        [self addSubview:iconButton];
        
        aliasLable = [[UILabel alloc]initWithFrame:CGRectMake(60.0f, margin_top-2.0, 150.0f, 25.0f)];
        [aliasLable setTextColor:[UIColor colorWithRed:0.720 green:0.225 blue:0.165 alpha:1.000]];
        [aliasLable setFont:[UIFont systemFontOfSize:16.0]];
        [aliasLable setContentMode:UIViewContentModeScaleAspectFit];
        [aliasLable setBackgroundColor:[UIColor clearColor]];
        [aliasLable setAdjustsFontSizeToFitWidth:YES];
        [self addSubview:aliasLable];
        
        timeLbl = [[UILabel alloc]initWithFrame:CGRectMake(60.0f, margin_top+22, 150.0f, 18.0f)];
        [timeLbl setTextColor:[UIColor grayColor]];
        [timeLbl setFont:[UIFont systemFontOfSize:14.0]];
        [timeLbl setContentMode:UIViewContentModeScaleAspectFit];
        [timeLbl setBackgroundColor:[UIColor clearColor]];
        [timeLbl setAdjustsFontSizeToFitWidth:YES];
        [self addSubview:timeLbl];
        
        contentLbl = [[UITextView alloc] initWithFrame:CGRectMake(52.0f,48.0f,200.0f,30.0f)];
        [contentLbl setEditable:NO];
        [contentLbl setDataDetectorTypes:UIDataDetectorTypeAll];
        [contentLbl setFont:[UIFont systemFontOfSize:16.0]];
        [contentLbl setTextColor:[UIColor blackColor]];
        [contentLbl setUserInteractionEnabled:YES];
        [contentLbl setBackgroundColor:[UIColor clearColor]];
        [self addSubview:contentLbl];
        
    }
    return self;
}

@end


@implementation ReplyView

- (id)initWithFrame:(CGRect)frame 
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

-(void)reloadReplyData:(NSArray *)replyArr withHeight:(CGFloat )height
{
    CGRect rect = self.frame;
    rect.size.height = height;
    self.frame =rect;
    
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    UIImage *bgImage = [[UIImage imageNamed:@"comment_bg"] stretchableImageWithLeftCapWidth:100 topCapHeight:15];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:bgImage];
    imageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height-5);
    [self addSubview:imageView];
    [imageView release];
    
    
    if (replyArr && [replyArr isKindOfClass:[NSArray class]] && [replyArr count]>0)
    {
        self.hidden = NO;
        
        CGFloat offset_y = 0.0f;
        for (int i = 0 ; i < [replyArr count]; i++)
        {
            NSDictionary *reply = [replyArr objectAtIndex:i];
            
            CGRect cellFrame = CGRectMake(0,offset_y,self.frame.size.width,0);
            ReplyViewCell *cell = [[ReplyViewCell alloc]initWithFrame:cellFrame];
            [cell.iconButton setImageWithURL:[NSURL URLWithString:[reply objectForKey:@"avatar"]] forState:UIControlStateNormal];
            [cell.aliasLable setText:[reply objectForKey:@"user_name"]];
            [cell.timeLbl setText:[reply objectForKey:@"create_time"]];
           
            NSString *replyContent = [MSTool trimmingWhiteSpaceCharacter:[reply objectForKey:@"content"]];
            [cell.contentLbl setText:replyContent];
            CGRect  contentFrame = cell.contentLbl.frame;
            contentFrame.size.height = cell.contentLbl.contentSize.height;
            cell.contentLbl.frame = contentFrame;
            
            cellFrame.size.height = 48.0f+cell.contentLbl.contentSize.height;
            cellFrame.origin.y = offset_y;
            cell.frame = cellFrame;
            
            offset_y += cellFrame.size.height;
            offset_y -= 15.0f;

            [self addSubview:cell];
            [cell release];
        }
    }
    else
    {
        self.hidden = YES;
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
